#include "general.h" 
#include "canvas.h"

BEGIN_EVENT_TABLE(CCanvas, wxPanel)
END_EVENT_TABLE()

CCanvas::CCanvas( 
	wxWindow *parent, wxWindowID id, 
	const wxPoint &position, const wxSize& size, long style 
	)
    : wxPanel( parent, id, position, size, style)
{
	m_pHead = new CHead(this);
	m_pFoot = new CFoot(this);
	m_pBody = new CBody(this);

	wxBoxSizer* pSizer = new wxBoxSizer(wxVERTICAL);
	pSizer->Add(m_pHead, 0, wxEXPAND);
	pSizer->Add(m_pBody, 1, wxEXPAND);
	pSizer->Add(m_pFoot, 0, wxEXPAND);

	pSizer->SetItemMinSize(m_pHead, 0, 50);
	pSizer->SetItemMinSize(m_pFoot, 0, 30);
	m_pFoot->SetBackgroundColour(wxColor(200, 200, 0));

	//struct
	//{
	//	wxString text; int id;
	//}
	//buttons_info[] = 
	//{
	//	{_T("button1"), BUTTON_1}, 
	//	{_T("button2"), BUTTON_2}, 
	//	{_T("button3"), BUTTON_3}, 
	//	{_T("button4"), BUTTON_4}, 
	//};
	//for (int i=0; i<sizeof(buttons_info)/sizeof(buttons_info[0]); i++)
	//{
	//	wxButton* pButton = new wxButton(this, buttons_info[i].id, buttons_info[i].text);
	//	wxSizerItem* pSizerItem = pSizer->Add(pButton, 0, wxALIGN_CENTER|wxALIGN_CENTER_VERTICAL);
	//}
	//pSizer->SetMinSize(0, 50);

	SetSizer(pSizer);

}

CCanvas::~CCanvas(void)
{
}

void CCanvas::SetImage(wxImage image)
{
	m_pBody->SetImage(image);
}

void CCanvas::SetZoomScale(float fZoomScale)
{
	m_pBody->SetZoomScale(fZoomScale);
}


////////////////////////////////////////////////////////
/// CHead
////////////////////////////////////////////////////////

BEGIN_EVENT_TABLE(CCanvas::CHead, wxPanel)
END_EVENT_TABLE()

CCanvas::CHead::CHead( 
	wxWindow *parent, wxWindowID id, 
	const wxPoint &position, const wxSize& size, long style 
	)
    : wxPanel( parent, id, position, size, style)
{
	m_pSizer = new wxBoxSizer(wxHORIZONTAL);

	m_pButtonUndo = new wxButton(this, ID_BUTTON_UNDO, _T("undo"));
	m_pSliderZoom = new wxSlider(this, ID_SLIDER_ZOOM, 100, 100, 200);

	m_pSizer->Add(m_pButtonUndo, 0, wxALIGN_CENTER|wxALIGN_CENTER_VERTICAL);
	m_pSizer->Add(m_pSliderZoom, 0, wxALIGN_CENTER|wxALIGN_CENTER_VERTICAL);

	SetSizer(m_pSizer);

	Connect(wxEVT_PAINT, wxPaintEventHandler(CCanvas::CHead::OnPaint));
	Connect(ID_SLIDER_ZOOM, wxEVT_COMMAND_SLIDER_UPDATED, 
		wxScrollEventHandler(CHead::OnScroll));  
}


CCanvas::CHead::~CHead(void)
{
}


void CCanvas::CHead::OnPaint(wxPaintEvent& event)
{
	wxPaintDC dc(this);

	wxRect rect = GetClientRect();

	dc.SetBrush(wxBrush(wxColor(200, 200, 0)));
	dc.DrawRoundedRectangle(rect, 10);
}

void CCanvas::CHead::OnScroll(wxScrollEvent& event)
{
	CCanvas* pParent = (CCanvas*)GetParent();
	pParent->SetZoomScale(((float)event.GetPosition() / 100));
	
}


////////////////////////////////////////////////////////
/// CBody
////////////////////////////////////////////////////////

BEGIN_EVENT_TABLE(CCanvas::CBody, wxPanel)
	EVT_ERASE_BACKGROUND(CCanvas::CBody::OnEraseBackGround)
	EVT_SIZE(CCanvas::CBody::OnSize)
END_EVENT_TABLE()

CCanvas::CBody::CBody( 
	wxWindow *parent, wxWindowID id, 
	const wxPoint &position, const wxSize& size, long style 
	)
    : wxPanel( parent, id, position, size, style)
{
	m_pImageNavigator = new CImageNavigator(this);
	m_pImageNavigator->SetSize(150, 150);

	m_fZoomScale = 1.0F;
	Connect(wxEVT_PAINT, wxPaintEventHandler(CCanvas::CBody::OnPaint));
}


CCanvas::CBody::~CBody(void)
{
}

void CCanvas::CBody::OnEraseBackGround(wxEraseEvent& event)
{
}

void CCanvas::CBody::OnPaint(wxPaintEvent& event)
{
	wxPaintDC dc(this);
	if (m_Image.Ok())
	{
		wxBitmap bitmap(m_Cache);
		dc.DrawBitmap(bitmap, 0, 0);
	}
}

void CCanvas::CBody::OnSize(wxSizeEvent& event)
{
	RepositionImageNavigator();

	BuildCache();
	Refresh();
}

void CCanvas::CBody::SetImage(wxImage image)
{
	m_Image = image;
	BuildCache();
	Refresh();
}

void CCanvas::CBody::SetZoomScale(float fZoomScale)
{
	m_fZoomScale = fZoomScale;
	BuildCache();
	Refresh();
}

void CCanvas::CBody::BuildCache()
{
	if (m_Cache.Ok())
		m_Cache.Destroy();

	wxSize size;
	size = GetClientSize();
	size = size.Scale(m_fZoomScale, m_fZoomScale);
	if (m_Image.Ok())
	{
		m_Cache = m_Image.Scale(size.GetWidth(), size.GetHeight());
	}
	else
	{
		m_Cache.Create(size.GetWidth(), size.GetHeight());
	}
}

void CCanvas::CBody::RepositionImageNavigator()
{
	if (m_pImageNavigator)
	{
		wxSize size1 = GetClientSize();
		wxSize size2 = m_pImageNavigator->GetClientSize();
		m_pImageNavigator->Move(size1.GetWidth() - size2.GetWidth() - 5, size1.GetHeight() - size2.GetHeight() - 5);
	}
}

////////////////////////////////////////////////////////
/// CFoot
////////////////////////////////////////////////////////

BEGIN_EVENT_TABLE(CCanvas::CFoot, wxPanel)
END_EVENT_TABLE()

CCanvas::CFoot::CFoot( 
	wxWindow *parent, wxWindowID id, 
	const wxPoint &position, const wxSize& size, long style 
	)
    : wxPanel( parent, id, position, size, style)
{
}


CCanvas::CFoot::~CFoot(void)
{
}


void CCanvas::CFoot::OnPaint(wxPaintEvent& event)
{
	wxPaintDC dc(this);
}


////////////////////////////////////////////////////////
/// CImageNavigator
////////////////////////////////////////////////////////

BEGIN_EVENT_TABLE(CCanvas::CImageNavigator, wxWindow)
	EVT_ERASE_BACKGROUND(CCanvas::CImageNavigator::OnEraseBackGround)
END_EVENT_TABLE()

CCanvas::CImageNavigator::CImageNavigator( 
	wxWindow *parent, wxWindowID id, 
	const wxPoint &position, const wxSize& size, long style 
	)
    : wxWindow( parent, id, position, size, style)
{
	SetBackgroundColour(wxColor(147, 181, 222));
	Refresh();
}


CCanvas::CImageNavigator::~CImageNavigator(void)
{
}


void CCanvas::CImageNavigator::OnEraseBackGround(wxEraseEvent& event)
{
	//wxDC* pDC = event.GetDC();
	//wxBrush brush(wxColor(147, 181, 222));
	//pDC->SetBrush(brush);
	//pDC->SetBackgroundMode
	//pDC->DrawRectangle(GetClientRect());
}

void CCanvas::CImageNavigator::OnPaint(wxPaintEvent& event)
{
	wxPaintDC dc(this);
	dc.Clear();

}
